<?php

/**
 * oRs
 *
 * @version 3.0
 * @copyright 2009 Ron Planken
 * @class: Class Roles
 * @description: Handles the actions for Class Roles
 */
class class_roles {

	private $_system;
	
	function __construct($system) {
		$this->_system = $system;
	}

	function get($class_role_id = -1, $class_id = -1, $role_id = -1) {

		if ($class_role_id == -1) {
			$query ="SELECT  cr.id
							,cr.class_id
							,cr.role_id
							,cr.name as 'class_role_name'
							,c.small_picture as 'class_picture'
							,c.name as 'class_name'
							,r.name as 'role_name'
							,r.small_picture as 'role_picture'
					   FROM class_role cr
					   JOIN class c ON c.class_id = cr.class_id
					   JOIN role r ON r.id = cr.role_id
					  WHERE r.enabled = 1
				   ORDER by cr.role_id";
		} else {
			
			$class_role_id = $this->_system->_db->secure_int($class_role_id);
			$class_id = $this->_system->_db->secure_int($class_id);
			$role_id = $this->_system->_db->secure_int($role_id);
			
			$query ="SELECT  cr.id
							,cr.class_id
							,cr.role_id
							,cr.name as 'class_role_name'
							,c.small_picture as 'class_picture'
							,c.name as 'class_name'
							,r.name as 'role_name'
							,r.small_picture as 'role_picture'
					   FROM class_role cr
					   JOIN class c ON c.class_id = cr.class_id
					   JOIN role r ON r.id = cr.role_id
							";
			if ($class_id > 0) {
				$query .= " AND cr.class_id = $class_id";
			}
			if ($role_id > 0) {
				$query .= " AND cr.role_id = $role_id";
			}
		}

		$class_roles = $this->_system->_db->selectMultiQuery($query);

		return $class_roles;
	}

	function getRoles($role_id = -1) {

		if ($role_id == -1) {
			$query ="SELECT  r.id
							,r.name as 'role_name'
							,r.small_picture as 'role_picture'
					   FROM role r
					  WHERE r.enabled = 1
				   ORDER by r.id";
		} else {
			$query ="SELECT  r.id
							,r.name as 'role_name'
							,r.small_picture as 'role_picture'
					   FROM role r
					  WHERE r.enabled = 1
							";
			if ($role_id > 0) {
				$role_id = $this->_system->_db->secure_int($role_id);
				
				$query .= " AND r.id = $role_id";
			}
		}

		$roles = $this->_system->_db->selectMultiQuery($query);

		return $roles;
	}
	
	function getfullClasses() {

		$query ="SELECT class_id as 'id'
						,name
						,small_picture
						,color_code
				   FROM class";

		$classes = $this->_system->_db->selectMultiQuery($query);

		return $classes;
	}
	
	function getClasses() {

		$query ="SELECT class_id as 'id'
						,name
				   FROM class";

		$classes = $this->_system->_db->selectSingleTextQuery($query);

		return $classes;
	}
	
	
	function save($player_id, $user_id, $name, $class_id, $race, $level, $team_id, $remover_id = 0, $date_removed = "Now()", $removed = 0) {

		$player_id = $this->_system->_db->secure_int($player_id);
		$user_id = $this->_system->_db->secure_int($user_id);
		$name = $this->_system->_db->secure_string( $name);
		$class = $this->_system->_db->secure_int( $class);
		$race = $this->_system->_db->secure_string($race );
		$level = $this->_system->_db->secure_int( $level);
		$team_id = $this->_system->_db->secure_int( $team_id);
		$remover_id = $this->_system->_db->secure_int( $remover_id);
		$date_removed = $this->_system->_db->secure_string( $date_removed);
		$removed = $this->_system->_db->secure_int( $removed);

		if ($player_id == 0) {
			$query ="INSERT INTO player
						   ( user_id
							,name
							,class_id
							,race
							,level
							,date_created
							,date_removed
							,remover_id
							,removed
							,guild_id
							,team_id)
					VALUES ( $user_id
							,$name
							,$class_id
							,$race
							,$level
							,Now()
							,0
							,0
							,0
							,0
							,1)
							";
			$status = $this->_system->_db->InsertQuery($query);

			$status = mysql_insert_id();

		} else {
			$query ="UPDATE player
						SET  name = $name
							,class_id = $class_id
							,race = $race
							,level = $level
							,date_removed = $date_removed
							,remover_id = $remover_id
							,removed = $removed
							,guild_id = 0
							,team_id = $team_id
					  WHERE player_id = $player_id
							";

			$status = $this->_system->_db->UpdateQuery($query);
		}

		return $status;

	}

	function remove($player_id = 0) {

		$remover_id = $_SESSION['uid'];
		
		$player_id = $this->_system->_db->secure_int($player_id);

		$query ="UPDATE player
						SET removed = 1
						   ,date_removed = Now()
						   ,remover_id = $remover_id
					  WHERE player_id = $player_id
							";
		$status =  $this->_system->_db->UpdateQuery($query);

		return $status;
	}

}
?>